package robot

import (
	"fmt"
	"strings"
)

// Command 命令.
type Command struct {
	// Names 名称.
	Names []string
	// Usage 用法.
	Usage string
	// 命令.
	Call func(string) string
}

// String .
func (c *Command) String() string {
	h := fmt.Sprintf("%s(%s)", c.Names[0], strings.Join(c.Names[1:], ", "))

	if c.Usage == "" {
		return h
	}

	return h + ": " + c.Usage
}

// Hit 命中.
func (c *Command) Hit(str string) bool {
	if str == "" {
		return false
	}

	s := strings.TrimSpace(strings.Split(str, " ")[0])

	for _, n := range c.Names {
		if len(n) == 1 {
			r := []rune(n)[0]
			if r > 32 && r < 65 {
				if n[0] == s[0] {
					return true
				}
			}
		}

		if n == s {
			return true
		}
	}

	return false
}
